package com.practicesuite.controllayer;

import javax.swing.ButtonGroup;
import javax.swing.DefaultComboBoxModel;
import javax.swing.JComboBox;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.JLabel;

import java.awt.Dimension;
import java.awt.GridLayout;
import javax.swing.JButton;
import javax.swing.SwingConstants;
import java.awt.Font;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;

import javax.swing.JScrollPane;
import javax.swing.ScrollPaneConstants;
import com.practicesuite.systemlayer.MedicalInformation;
import com.practicesuite.systemlayer.Patient;
import com.practicesuite.systemlayer.SystemDriver;
import com.practicesuite.systemlayer.User;
import javax.swing.JRadioButton;

/**
 * Panel for viewing and editing a specific user's information.
 * 
 * @author James
 *
 */
public class EditUserPanel extends JPanel {
    private static final long serialVersionUID = 1L;
    private JTextField textFieldName;
    private JTextField textFieldAddress;
    private JTextField textFieldPhone;
    private JTextField textFieldEmail;
    private JTextField textFieldAge;
    private JTextField textFieldPharmacy;
    private JTextField textFieldInsurance;
    private JTextField textFieldAllergies;
    public MainWindow window;
    private MedicalInformation medicalInformation;
    private JTextField textFieldPassword;

    /**
     * Create the panel.
     */
    public EditUserPanel(MainWindow container, final User targetUser, final User user) {
        this.window = container;
        setLayout(new GridLayout(0, 1, 0, 0));
        setPreferredSize(new Dimension(500, 400));
        
        if (targetUser.getEUID() == 3) {
            Patient p = (Patient)targetUser;
            medicalInformation = p.getMedicalInformation();
        }
        
        JPanel panel_8 = new JPanel();
        panel_8.setLayout(new GridLayout(0, 1, 0, 0));
        panel_8.setPreferredSize(new Dimension(500, 450));
        
        JLabel labelHeader = new JLabel("Edit User Information");
        panel_8.add(labelHeader);
        labelHeader.setFont(new Font("Tahoma", Font.PLAIN, 24));
        labelHeader.setHorizontalAlignment(SwingConstants.CENTER);
        
        JPanel panel_6 = new JPanel();
        panel_8.add(panel_6);
        panel_6.setLayout(new GridLayout(0, 2, 0, 0));
        
        JLabel lblUsername = new JLabel("User ID: ");
        lblUsername.setHorizontalAlignment(SwingConstants.RIGHT);
        lblUsername.setFont(new Font("Tahoma", Font.PLAIN, 12));
        panel_6.add(lblUsername);
        
        JLabel label = new JLabel(Integer.toString(targetUser.getUserID()));
        lblUsername.setFont(new Font("Tahoma", Font.PLAIN, 12));
        panel_6.add(label);
        
        JLabel lblPassword = new JLabel("Password: ");
        lblPassword.setFont(new Font("Tahoma", Font.PLAIN, 12));
        lblPassword.setHorizontalAlignment(SwingConstants.RIGHT);
        panel_6.add(lblPassword);
        
        //TODO need getPassword method for User class
        textFieldPassword = new JTextField(/*targetUser.getPassword()*/);
        textFieldPassword.setFont(new Font("Tahoma", Font.PLAIN, 12));
        panel_6.add(textFieldPassword);
        textFieldPassword.setColumns(10);
        
        JPanel panel_12 = new JPanel();
        panel_8.add(panel_12);
        panel_12.setLayout(new GridLayout(0, 2, 0, 0));
        
        final JRadioButton rdbtnUnsuspended = new JRadioButton("Unsuspended");
        rdbtnUnsuspended.setFont(new Font("Tahoma", Font.PLAIN, 12));
        rdbtnUnsuspended.setHorizontalAlignment(SwingConstants.RIGHT);
        panel_12.add(rdbtnUnsuspended);
        
        final JRadioButton rdbtnSuspended = new JRadioButton("Suspended");
        rdbtnSuspended.setFont(new Font("Tahoma", Font.PLAIN, 12));
        panel_12.add(rdbtnSuspended);
        
        ButtonGroup group = new ButtonGroup();
        group.add(rdbtnUnsuspended);
        group.add(rdbtnSuspended);
        
        JPanel panel_13 = new JPanel();
        panel_12.add(panel_13);
        
        JPanel panel = new JPanel();
        panel_8.add(panel);
        panel.setLayout(new GridLayout(0, 2));
        
        JLabel labelFirstName = new JLabel("Name: ");
        labelFirstName.setFont(new Font("Tahoma", Font.PLAIN, 12));
        labelFirstName.setHorizontalAlignment(SwingConstants.RIGHT);
        panel.add(labelFirstName);
        
        textFieldName = new JTextField(targetUser.getName());
        textFieldName.setFont(new Font("Tahoma", Font.PLAIN, 12));
        panel.add(textFieldName);
        textFieldName.setColumns(10);
        
        JLabel labelUserType = new JLabel("User Type: ");
        labelUserType.setHorizontalAlignment(SwingConstants.RIGHT);
        labelUserType.setFont(new Font("Tahoma", Font.PLAIN, 12));
        panel.add(labelUserType);
        
        final JComboBox comboBoxUserType = new JComboBox();
        comboBoxUserType.setFont(new Font("Tahoma", Font.PLAIN, 12));
        comboBoxUserType.setModel(new DefaultComboBoxModel(new String[] {"Admininstrator", "Doctor", "Nurse", "Patient"}));
        panel.add(comboBoxUserType);
        comboBoxUserType.setSelectedIndex(targetUser.getEUID());
        
        JPanel panel_1 = new JPanel();
        panel_8.add(panel_1);
        panel_1.setLayout(new GridLayout(0, 2, 0, 0));
        
        JLabel labelAddress = new JLabel("Street Address: ");
        panel_1.add(labelAddress);
        labelAddress.setFont(new Font("Tahoma", Font.PLAIN, 12));
        labelAddress.setHorizontalAlignment(SwingConstants.RIGHT);
        
        textFieldAddress = new JTextField(targetUser.getAddress());
        textFieldAddress.setFont(new Font("Tahoma", Font.PLAIN, 12));
        panel_1.add(textFieldAddress);
        textFieldAddress.setColumns(10);
        
        JLabel labelPhone = new JLabel("Phone Number [(###) ###-####]: ");
        panel_1.add(labelPhone);
        labelPhone.setFont(new Font("Tahoma", Font.PLAIN, 12));
        labelPhone.setHorizontalAlignment(SwingConstants.RIGHT);
        
        textFieldPhone = new JTextField(targetUser.getPhoneNumber());
        textFieldPhone.setFont(new Font("Tahoma", Font.PLAIN, 12));
        panel_1.add(textFieldPhone);
        textFieldPhone.setColumns(10);
        
        JPanel panel_2 = new JPanel();
        panel_8.add(panel_2);
        panel_2.setLayout(new GridLayout(0, 2, 0, 0));
        
        JLabel labelEmail = new JLabel("Email Address: ");
        panel_2.add(labelEmail);
        labelEmail.setFont(new Font("Tahoma", Font.PLAIN, 12));
        labelEmail.setHorizontalAlignment(SwingConstants.RIGHT);
        
        textFieldEmail = new JTextField(targetUser.getEmail());
        textFieldEmail.setFont(new Font("Tahoma", Font.PLAIN, 12));
        panel_2.add(textFieldEmail);
        textFieldEmail.setColumns(10);
        
        JLabel labelGender = new JLabel("Gender: ");
        panel_2.add(labelGender);
        labelGender.setFont(new Font("Tahoma", Font.PLAIN, 12));
        labelGender.setHorizontalAlignment(SwingConstants.RIGHT);
        
        final JComboBox comboBoxGender = new JComboBox();
        comboBoxGender.setFont(new Font("Tahoma", Font.PLAIN, 12));
        comboBoxGender.setModel(new DefaultComboBoxModel(new String[] {"Male", "Female"}));
        if(targetUser.getGender().equals("male"))
            comboBoxGender.setSelectedIndex(0);
        else
            comboBoxGender.setSelectedIndex(1);
        panel_2.add(comboBoxGender);
        
        JPanel panel_4 = new JPanel();
        panel_8.add(panel_4);
        panel_4.setLayout(new GridLayout(0, 2, 0, 0));
        
        JLabel labelAge = new JLabel("Age: ");
        panel_4.add(labelAge);
        labelAge.setFont(new Font("Tahoma", Font.PLAIN, 12));
        labelAge.setHorizontalAlignment(SwingConstants.RIGHT);
        
        textFieldAge = new JTextField(Integer.toString(targetUser.getAge()));
        textFieldAge.setFont(new Font("Tahoma", Font.PLAIN, 12));
        panel_4.add(textFieldAge);
        textFieldAge.setColumns(10);
        
        JLabel labelPharmacy = new JLabel("Pharmacy: ");
        panel_4.add(labelPharmacy);
        labelPharmacy.setHorizontalAlignment(SwingConstants.RIGHT);
        labelPharmacy.setFont(new Font("Tahoma", Font.PLAIN, 12));
        
        textFieldPharmacy = new JTextField((targetUser.getEUID() == 3) ? medicalInformation.getPharmCarrier() : null);
        textFieldPharmacy.setFont(new Font("Tahoma", Font.PLAIN, 12));
        panel_4.add(textFieldPharmacy);
        textFieldPharmacy.setColumns(10);
        
        JPanel panel_3 = new JPanel();
        panel_8.add(panel_3);
        panel_3.setLayout(new GridLayout(0, 2, 0, 0));
        
        JLabel labelInsurance = new JLabel("Insurance Carrier: ");
        panel_3.add(labelInsurance);
        labelInsurance.setHorizontalAlignment(SwingConstants.RIGHT);
        labelInsurance.setFont(new Font("Tahoma", Font.PLAIN, 12));
        
        textFieldInsurance = new JTextField((targetUser.getEUID() == 3) ? medicalInformation.getInsurance() : null);
        textFieldInsurance.setFont(new Font("Tahoma", Font.PLAIN, 12));
        panel_3.add(textFieldInsurance);
        textFieldInsurance.setColumns(10);
        
        JLabel labelAllergies = new JLabel("Allergies: ");
        panel_3.add(labelAllergies);
        labelAllergies.setHorizontalAlignment(SwingConstants.RIGHT);
        labelAllergies.setFont(new Font("Tahoma", Font.PLAIN, 12));
        
        textFieldAllergies = new JTextField((targetUser.getEUID() == 3) ? medicalInformation.getAllergies().toString() : null);
        textFieldAllergies.setFont(new Font("Tahoma", Font.PLAIN, 12));
        panel_3.add(textFieldAllergies);
        textFieldAllergies.setColumns(10);
        
        JPanel panel_5 = new JPanel();
        panel_8.add(panel_5);
        
        JButton btnSignup = new JButton("Save Changes");
        btnSignup.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent arg0) {
                try {
                    char[] password = textFieldPassword.getText().toCharArray();
                    String name = textFieldName.getText();
                    int userTypeIndex = comboBoxUserType.getSelectedIndex();
                    String address = textFieldAddress.getText();
                    String phoneNumber = textFieldPhone.getText();
                    String email = textFieldEmail.getText();
                    String gender = (String)comboBoxGender.getSelectedItem();
                    int age = Integer.parseInt(textFieldAge.getText());
                    String pharmacy = textFieldPharmacy.getText();
                    String insurance = textFieldInsurance.getText();
                    String allergies = textFieldAllergies.getText();
                    MedicalInformation medicalInfo = new MedicalInformation(90, pharmacy, allergies, insurance);
                    
                    Boolean suspended = targetUser.isSuspended();
                    if (rdbtnUnsuspended.isSelected()) {
                        suspended = false;
                    }
                    
                    try {
                        User newUser = new User(name, userTypeIndex, targetUser.getUserID(), targetUser.getDateOfBirth(), 
                                address, phoneNumber, gender, age, email, suspended);
                        if(password.length == 0) {
                            SystemDriver.editUser(newUser, targetUser.getUserID(), userTypeIndex, suspended);
                            }
                        else {
                            SystemDriver.editUser(newUser, targetUser.getUserID(), userTypeIndex, password, suspended);
                        }
                        if (userTypeIndex == 3) {
                            Patient newPatient = new Patient(name, userTypeIndex, targetUser.getUserID(), targetUser.getDateOfBirth(), 
                                    address, phoneNumber, gender, age, medicalInfo, email, suspended);
                            SystemDriver.updatePatient(newPatient);
                        }
                        if (rdbtnSuspended.isSelected()) {
                            SystemDriver.suspendAccount(targetUser, userTypeIndex, targetUser.getUserID());
                        }
                        JOptionPane.showMessageDialog(null, "User updated successfully.");
                        window.gotoPanel("adminHome", user);
                    } catch (Exception e) {
                        JOptionPane.showMessageDialog(null, "System error, contact administrator.");
                    }
                } catch (Exception e) {
                    JOptionPane.showMessageDialog(null, "Invalid input.");
                }
            }
        });
        panel_5.add(btnSignup);
        btnSignup.setFont(new Font("Tahoma", Font.PLAIN, 14));
        
        JButton btnReturn = new JButton("Cancel");
        btnReturn.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent arg0) {
                window.gotoPanel("adminHome", user);
            }
        });
        btnReturn.setFont(new Font("Tahoma", Font.PLAIN, 14));
        panel_5.add(btnReturn);
        
        JScrollPane scrollPane = new JScrollPane(panel_8);
        scrollPane.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
        scrollPane.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
        add(scrollPane);
    }
}
